package com.sheliduan.itlearning.admin.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sheliduan.itlearning.admin.model.vos.category.AddCategoryReqVO;
import com.sheliduan.itlearning.admin.model.vos.category.DeleteCategoryReqVO;
import com.sheliduan.itlearning.admin.model.vos.category.FindCategoryPageListReqVO;
import com.sheliduan.itlearning.admin.model.vos.tag.UpdateTagReqVO;
import com.sheliduan.itlearning.admin.service.AdminCategoryService;
import com.sheliduan.itlearning.common.domain.dos.CategoryDO;
import com.sheliduan.itlearning.common.domain.dos.TagDO;
import com.sheliduan.itlearning.common.domain.mapper.CategoryMapper;
import com.sheliduan.itlearning.common.domain.mapper.TagMapper;
import com.sheliduan.itlearning.common.enums.ErrorCodeEnum;
import com.sheliduan.itlearning.common.exception.BizException;
import com.sheliduan.itlearning.common.utils.PageResponseData;
import com.sheliduan.itlearning.common.utils.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * @author: 断舍离
 * @date: 2025/2/18 10:09
 * @description: 后台管理员分类Service实现类
 **/
@Service
public class AdminCategoryServiceImpl implements AdminCategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private TagMapper tagMapper;

    @Override
    public PageResponseData findCategoryPageList(FindCategoryPageListReqVO reqVO) {
        long current = reqVO.getCurrent();
        long size = reqVO.getSize();
        String name = reqVO.getName();
        LocalDateTime startDate = reqVO.getStartDate();
        LocalDateTime endDate = reqVO.getEndDate();
        Page<CategoryDO> page = categoryMapper.selectPageList(current, size, name, startDate, endDate);
        List<CategoryDO> categoryDOS = page.getRecords();
        return PageResponseData.success(page, categoryDOS);
    }

    @Override
    public ResponseData addCategory(AddCategoryReqVO reqVO) {
        CategoryDO categoryDO = CategoryDO.builder()
                .name(reqVO.getName())
                .createTime(new Date())
                .updateTime(new Date())
                .build();
        int i = 0;
        // 由于数据库分类名是唯一的, 若插入已经存在的可能报错, 直接抛出异常即可
        try {
            i = categoryMapper.insert(categoryDO);
        }catch (Exception e){
            throw new BizException(ErrorCodeEnum.CATEGORY_IS_EXIST);
        }
        if(i > 0)
            return ResponseData.success();
        else
            return ResponseData.fail("新增分类失败");
    }

    @Override
    @Transactional
    public ResponseData deleteCategory(DeleteCategoryReqVO reqVO) {
        CategoryDO categoryDO = categoryMapper.selectById(reqVO.getId());
        if(categoryDO.getArticleTotal()+categoryDO.getVideoTotal() != 0) {
            return ResponseData.fail("该分类下还有课程, 无法删除");
        }
        // 判断该分类下是否还有标签
        List<TagDO> tagDOS = tagMapper.selectByCategoryId(reqVO.getId());
        if(!tagDOS.isEmpty()) {
            return ResponseData.fail("该分类下还有标签, 无法删除");
        }
        // 删除分类
        int i = categoryMapper.deleteById(reqVO.getId());
        if(i > 0)
            return ResponseData.success();
        else
            return ResponseData.fail("删除分类失败");
    }

    @Override
    public ResponseData updateCategory(UpdateTagReqVO reqVO) {
        int i = categoryMapper.updateCategory(reqVO.getId(), reqVO.getName());
        if(i > 0)
            return ResponseData.success();
        else
            return ResponseData.fail("修改分类失败");
    }

    @Override
    public ResponseData selectById(Integer id) {
        CategoryDO categoryDO = categoryMapper.selectById(id);
        if(categoryDO != null)
            return ResponseData.success(categoryDO);
        else
            return ResponseData.fail("该分类不存在");
    }
}
